/**
 * Copyright (c) 2014- 2015 YCY Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.jkwzms.system.organization.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.jkwzms.common.Constants;
import com.jkwzms.common.inject.annotation.BaseComponent;
import com.jkwzms.common.plugin.web.controller.BaseTreeableController;
import com.jkwzms.system.organization.entity.Organization;
import com.jkwzms.system.organization.entity.OrganizationType;
import com.jkwzms.system.organization.service.OrganizationService;

/**
 * <p>
 * User: Jack Liang
 * <p>
 * Date: 13-1-28 下午4:29
 * <p>
 * Version:1.0.0
 */
@Controller
@RequestMapping(value = "/sys/organization/organization")
public class OrganizationController extends BaseTreeableController<Organization, Long> {

    @Autowired
    @BaseComponent
    private OrganizationService organizationService;

    public OrganizationController(){
        setResourceIdentity("sys:organization");
    }

    @Override
    protected void setCommonData(Model model) {
        super.setCommonData(model);
        model.addAttribute("types", OrganizationType.values());
    }

    @RequestMapping(value = "/changeStatus/{newStatus}")
    public String changeStatus(HttpServletRequest request, @PathVariable("newStatus") Boolean newStatus,
                               @RequestParam("ids") Long[] ids, RedirectAttributes redirectAttributes) {

        this.permissionList.assertHasUpdatePermission();

        for (Long id : ids) {
            Organization organization = organizationService.findOne(id);
            organization.setShow(newStatus);
            organizationService.update(organization);
        }

        redirectAttributes.addFlashAttribute(Constants.MESSAGE, "操作成功！");

        return "redirect:" + request.getAttribute(Constants.BACK_URL);
    }

    /**
     * 查看跳转
     */
    @RequestMapping(value = "list")
    public String list(HttpServletRequest request, Model model) {
        List<Organization> entity = organizationService.findAll();
        model.addAttribute("entity", entity);
        return "sys/organization/selectOrganizationAndJob";
    }

    /**
     * 查看跳转
     */
    @RequestMapping(value = "listgrid")
    @ResponseBody
    public List<Organization> listGrid(HttpServletRequest request, Model model) {
        List<Organization> entity = organizationService.findAll();
        List<Organization> resource = new ArrayList<Organization>();
        for (int i = 0; i < entity.size(); i++) {
            if (entity.get(i).isRoot()) {
                Organization res = new Organization();
                res.setId(entity.get(i).getId());
                res.setChildren(entity.get(i).getChildren());
                res.setIcon(entity.get(i).getIcon());
                res.setName(entity.get(i).getName());
                res.setParentId(entity.get(i).getParentId());
                res.setParentIds(entity.get(i).getParentIds());
                res.setType(entity.get(i).getType());
                resource.add(res);
            }
        }

        return resource;
    }

    /**
     * 添加
     * 
     * @param request
     */
    @RequestMapping("add")
    public String add(HttpServletRequest request, Organization resource) {
        resource.setParentIds(resource.getParentId().toString());
        organizationService.save(resource);
        return "sys/organization/selectOrganizationAndJob.jsp?v=" + new Date();
    }

    /**
     * 更新
     * 
     * @param request
     */
    @RequestMapping("update")
    public String update(HttpServletRequest request, Organization resource) {
        resource.setParentIds(resource.getParentId().toString());
        organizationService.update(resource);
        return "sys/organization/selectOrganizationAndJob.jsp?v=" + new Date();
    }

    @RequestMapping("deleteMessage/{id}")
    public String delete(@PathVariable("id") Long id) {
        organizationService.delete(id);
        return "sys/organization/selectOrganizationAndJob.jsp?v=" + new Date();
    }
}
